
#ifndef INITDB_H
#define INITDB_H

#include <QtSql>

void addBook(QSqlQuery &q, const QString &title, int year, int authorId,
             int genreId, int rating)
{
    static int id = 0;
    q.addBindValue(++id);
    q.addBindValue(title);
    q.addBindValue(year);
    q.addBindValue(authorId);
    q.addBindValue(genreId);
    q.addBindValue(rating);
    qDebug() << "addBook: q.exec = " << q.exec() << "\n";
}

int addGenre(QSqlQuery &q, const QString &name)
{
    static int id = 0;
    q.addBindValue(++id);
    q.addBindValue(name);
    auto ret = q.exec();
    qDebug() << "addGenre: q.exec = " << ret<< "\n";
    //qDebug() << "addGenre: q.lastInsertId = " << q.lastInsertId().toInt() << "\n";
    //return q.lastInsertId();
    return id;
}

int addAuthor(QSqlQuery &q, const QString &name, const QDate &birthdate)
{
    static int id = 0;
    q.addBindValue(++id);
    q.addBindValue(name);
    q.addBindValue(birthdate);
    auto ret = q.exec();
    qDebug() << "addGenre: q.exec = " << ret << "\n";
    //qDebug() << "addGenre: q.lastInsertId = " << q.lastInsertId().toInt() << "\n";
    //return q.lastInsertId();
    return id;
}

QSqlError initDb()
{
    //QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    //db.setDatabaseName(":memory:");
    //db.setDatabaseName("isr_reader_db.dat");

    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("192.168.189.129");
    db.setUserName("root");
    db.setPassword("123456");
    db.setDatabaseName("test");

    if (!db.open())
        return db.lastError();

    QStringList tables = db.tables();
    if (tables.contains("books", Qt::CaseInsensitive)
        || tables.contains("authors", Qt::CaseInsensitive))
        return QSqlError();

    QSqlQuery q;
    if (!q.exec(QLatin1String("create table books(id int primary key, title varchar(512), author int, genre int, year int, rating int);")))
        return q.lastError();
    if (!q.exec(QLatin1String("create table authors(id int primary key, name varchar(32), birthdate date);")))
        return q.lastError();
    if (!q.exec(QLatin1String("create table genres(id int primary key, name varchar(32));")))
        return q.lastError();

    if (!q.prepare(QLatin1String("insert into authors(id, name, birthdate) values(?, ?, ?);")))
        return q.lastError();
    auto asimovId = addAuthor(q, QLatin1String("Isaac Asimov"), QDate(1920, 2, 1));
    auto greeneId = addAuthor(q, QLatin1String("Graham Greene"), QDate(1904, 10, 2));
    auto pratchettId = addAuthor(q, QLatin1String("Terry Pratchett"), QDate(1948, 4, 28));

    //qDebug() << "asimovId = " <<asimovId.toInt() << ", greeneId = " << greeneId.toInt() << ", pratchettId = " << pratchettId.toInt() << "\n";

    if (!q.prepare(QLatin1String("insert into genres(id, name) values(?, ?);")))
        return q.lastError();
    auto sfiction = addGenre(q, QLatin1String("Science Fiction"));
    auto fiction = addGenre(q, QLatin1String("Fiction"));
    auto fantasy = addGenre(q, QLatin1String("Fantasy"));

    //qDebug() << "sfiction = " <<sfiction.toInt() << ", fiction = " << fiction.toInt() << ", fantasy = " << fantasy.toInt() << "\n";

    if (!q.prepare(QLatin1String("insert into books(id, title, year, author, genre, rating) values(?, ?, ?, ?, ?, ?);")))
        return q.lastError();
    addBook(q, QLatin1String("Foundation"), 1951, asimovId, sfiction, 3);
    addBook(q, QLatin1String("Foundation and Empire"), 1952, asimovId, sfiction, 4);
    addBook(q, QLatin1String("Second Foundation"), 1953, asimovId, sfiction, 3);
    addBook(q, QLatin1String("Foundation's Edge"), 1982, asimovId, sfiction, 3);
    addBook(q, QLatin1String("Foundation and Earth"), 1986, asimovId, sfiction, 4);
    addBook(q, QLatin1String("Prelude to Foundation"), 1988, asimovId, sfiction, 3);
    addBook(q, QLatin1String("Forward the Foundation"), 1993, asimovId, sfiction, 3);
    addBook(q, QLatin1String("The Power and the Glory"), 1940, greeneId, fiction, 4);
    addBook(q, QLatin1String("The Third Man"), 1950, greeneId, fiction, 5);
    addBook(q, QLatin1String("Our Man in Havana"), 1958, greeneId, fiction, 4);
    addBook(q, QLatin1String("Guards! Guards!"), 1989, pratchettId, fantasy, 3);
    addBook(q, QLatin1String("Night Watch"), 2002, pratchettId, fantasy, 3);
    addBook(q, QLatin1String("Going Postal"), 2004, pratchettId, fantasy, 3);

    return QSqlError();
}

#endif
